<?php
App::uses('AppController', 'Controller');
App::import('Model','Post');
/**
 * Frontpages Controller
 *
 * @property Frontpage $Frontpage
 */
class FrontpagesController extends AppController {
	public $helpers = array('Posts', 'Gravatar');
	var $layout = 'frontend';

	public function beforeFilter(){
	  parent::beforeFilter();
		$this->Auth->allow();
		Controller::loadModel('Post');
		Controller::loadModel('Parameter');
		
	}

	public function index(){
		if ($this->Session->check('Auth.User')){
			echo("Loginnnnnnnnnnnnnnnnnnn");
			var_dump($this->Auth->user());
		}
		$this->set('news', $this->Post->getNews());
		$this->set('travel', $this->Post->getRandTravel());
		$this->set('consult1', $this->Post->getConsultsByTopic(Post::consult1));
		$this->set('consult2', $this->Post->getConsultsByTopic(Post::consult2));
		$this->set('consult3', $this->Post->getConsultsByTopic(Post::consult3));
		$this->set('consult4', $this->Post->getConsultsByTopic(Post::consult4));
	}

	public function contact(){
		Controller::loadModel('Comment');
		$this->set('posts',$this->Post->getTravelsForRightPanel());
		if ($this->request->is('post') && $this->request->data['Comment']) {
			$this->Comment->create();
			if ($this->Comment->save($this->request->data)) {
				$this->Session->setFlash(__('Gửi thông điệp thành công !!!'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.'));
			}

			$comment = $this->Comment->read();
			App::uses('CakeEmail', 'Network/Email');
			$email = new CakeEmail('smtp');
			$email->template('ask_question');
			$email->emailFormat('html');
			$email->viewVars(array('comment' => $comment));
			$email->to('sitgroups@gmail.com');
			$email->subject('Khách hàng liên hệ');
			$email->send();
		}
	}

	public function viewPost($id){
		$this->Post->id = $id;
		if (!$this->Post->exists()) {
			throw new NotFoundException(__('Invalid post'));
		}
		$a = $this->Post->read(null, $id);
		$this->set('post', $this->Post->read(null, $id));
	}
}
